!! set up the initial configuration for the serial spectral code
!! obeying the K41 spectra. And also divergenceless. 
!! ---------------------------------------------------------------
subroutine iniconf2d
  use mod_2dflu
  implicit none
  double precision,dimension(3) :: ran
  double precision :: rk2,rk,ek1,vk,p11,p12,p31,p22,p33,p23,rk2inv
  double precision :: iniamp,omegak,n_sqr,rkini,mabs,prim,pert,x,y
  integer :: i1,i2,i3,k1,k2,k3,ksqr,mshl,ik,iseed,ireal,iimag,m1,m2
  integer,parameter :: nsize=1
  integer ::ns
  integer,dimension(nsize) :: seed
  integer,dimension(3) :: tarray
  integer,dimension(2) :: tofday
  integer :: ierr
  double precision :: rphi1,rphi2
  double precision :: omega_amp=0.1,komega=4.0d0
!! -----------------------------------------------------------------
!! vorticity has a flat spectra with random phase 
  n_sqr = dfloat(n1*n2)
  call itime(tarray)
  seed(1) = tarray(3)*9765 + tarray(2)*1734 + tarray(3)
!  write(*,*) 'iseed-',iseed
  call init_random_seed()
  call random_number(rphi1); call random_number(rphi2)
  rphi1 = rphi1*2*pi - pi
  rphi2 = rphi2*2*pi - pi
!! --------- 
  do i2 = 1,n2
    y = i2*dx
    do i1 = 1,n1
      x = i1*dx
     !
      prim = dcos(komega*y+rphi1) + dcos(komega*x+rphi2)
      pert = 0.0d0
      do m2 = 0,2!Nx/2
        do m1 = 0,2!Ny/2
          mabs = dsqrt(dble(m1*m1 + m2*m2))
          !
          if(m1**2+m2**2.ne.0)then
            pert = pert + &
              1.0d-4*(dsin(m1*x+m2*y) + dcos(m1*x+m2*y))*dble(m2*m2)/mabs
          endif
          !
        enddo
      enddo
!        omega(i1,i2) = (prim + pert)*vis
      omega(i1,i2) = (prim + pert)*omega_amp*komega
    enddo
  enddo
!--------------------------------------------
end subroutine iniconf2d

subroutine init_random_seed 
            INTEGER :: i, n, clock
            INTEGER, DIMENSION(:), ALLOCATABLE :: seed
             n=1

            CALL RANDOM_SEED(size = n)
            ALLOCATE(seed(n))

            CALL SYSTEM_CLOCK(COUNT=clock)

            seed = clock + 37 * (/ (i - 1, i = 1, n) /)
            CALL RANDOM_SEED(PUT = seed)

            DEALLOCATE(seed)
          END subroutine 

